package com.example.javafx.javafx.controller;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class MenuContentLayoutApp extends Application {

    private BorderPane root = new BorderPane();
    private StackPane contentArea = new StackPane();
    private Label titleLabel = new Label("欢迎页面");

    @Override
    public void start(Stage primaryStage) {
        // 1. 创建左侧菜单
        VBox leftMenu = createLeftMenu();

        // 2. 创建中间内容区域（带标题栏）
        BorderPane centerContent = createCenterContent();

        // 3. 设置整体布局
        root.setLeft(leftMenu);
        root.setCenter(centerContent);

        // 4. 设置场景和舞台
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setTitle("JavaFX布局示例");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private VBox createLeftMenu() {
        VBox menuBox = new VBox(10);
        menuBox.setPadding(new Insets(15));
        menuBox.setStyle("-fx-background-color: #f0f0f0;");

        // 菜单按钮
        Button homeBtn = new Button("首页");
        Button productsBtn = new Button("产品管理");
        Button ordersBtn = new Button("订单管理");
        Button settingsBtn = new Button("系统设置");

        // 设置按钮样式
        String buttonStyle = "-fx-min-width: 120px; -fx-min-height: 40px;";
        homeBtn.setStyle(buttonStyle);
        productsBtn.setStyle(buttonStyle);
        ordersBtn.setStyle(buttonStyle);
        settingsBtn.setStyle(buttonStyle);

        // 添加按钮事件
        homeBtn.setOnAction(e -> showContent("首页", createHomeContent()));
        productsBtn.setOnAction(e -> showContent("产品管理", createProductsContent()));
        ordersBtn.setOnAction(e -> showContent("订单管理", createOrdersContent()));
        settingsBtn.setOnAction(e -> showContent("系统设置", createSettingsContent()));

        menuBox.getChildren().addAll(homeBtn, productsBtn, ordersBtn, settingsBtn);
        return menuBox;
    }

    private BorderPane createCenterContent() {
        BorderPane centerPane = new BorderPane();

        // 顶部标题栏
        HBox titleBar = new HBox(titleLabel);
        titleBar.setPadding(new Insets(15));
        titleBar.setStyle("-fx-background-color: #e0e0e0; -fx-font-size: 18px;");

        // 中间内容区域
        contentArea.getChildren().add(createHomeContent());

        centerPane.setTop(titleBar);
        centerPane.setCenter(contentArea);
        return centerPane;
    }

    private void showContent(String title, Pane content) {
        titleLabel.setText(title);
        contentArea.getChildren().clear();
        contentArea.getChildren().add(content);
    }

    // 以下是各个页面的内容创建方法
    private Pane createHomeContent() {
        VBox homePane = new VBox(20, new Label("欢迎来到系统首页"));
        homePane.setPadding(new Insets(20));
        return homePane;
    }

    private Pane createProductsContent() {
        TableView<String> table = new TableView<>();
        // 这里添加表格列和数据...
        return new VBox(10, new Label("产品管理页面"), table);
    }

    private Pane createOrdersContent() {
        return new VBox(10, new Label("订单管理页面"),
                new ListView<>(javafx.collections.FXCollections.observableArrayList(
                        "订单1", "订单2", "订单3")));
    }

    private Pane createSettingsContent() {
        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.add(new Label("系统设置"), 0, 0);
        grid.add(new CheckBox("启用通知"), 0, 1);
        grid.add(new TextField(), 1, 1);
        return grid;
    }

    public static void main(String[] args) {
        launch(args);
    }
}